package com.lab303.powerproject.util;


import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.ClassUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadExcel {

    public static Map<String, List<String>> readExcel(File file, int num) {
        List<Map<String, List<String>>> data = new ArrayList<>();
        try {
            Workbook workbook = new XSSFWorkbook(file);
            if (num <= 2) {
                Sheet sheet1 = workbook.getSheetAt(0);
                Map<String, List<String>> winterData = new HashMap<>();
                for (int i = num * 7; i < num * 7 + 7; i++) {
                    String name = "";
                    switch (i % 7) {
                        case 0:
                            name = "temperature";
                            break;
                        case 1:
                            name = "nuantong";
                            break;
                        case 2:
                            name = "guangfu";
                            break;
                        case 3:
                            name = "xuneng";
                            break;
                        case 4:
                            name = "dimaiguan";
                            break;
                        case 5:
                            name = "diyuanrebeng";
                            break;
                        case 6:
                            name = "dianliang";
                            break;
                        default:
                    }
                    List<String> rowData = new ArrayList<>();
                    Row row = sheet1.getRow(i);
                    for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                        String cellInfo = row.getCell(j).toString();
                        if (cellInfo.isEmpty()) {
                            continue;
                        }
                        rowData.add(cellInfo);
                    }
                    winterData.put(name, rowData);

                }
                return winterData;
            } else {
                Sheet sheet2 = workbook.getSheetAt(1);
                Map<String, List<String>> summerData = new HashMap<>();
                for (int i = (num-3) * 11; i < (num-3) * 11 + 11; i++) {
                    String name = "";
                    switch (i % 11) {
                        case 0:
                            name = "temperature";
                            break;
                        case 1:
                            name = "humidity";
                            break;
                        case 2:
                            name = "nuantong";
                            break;
                        case 3:
                            name = "nuandian";
                            break;
                        case 4:
                            name = "guangfu";
                            break;
                        case 5:
                            name = "dimaiguan";
                            break;
                        case 6:
                            name = "xinfengjizu";
                            break;
                        case 7:
                            name = "xuneng";
                            break;
                        case 8:
                            name = "diyuanrebeng";
                            break;
                        case 9:
                            name = "zhujixuneng";
                            break;
                        case 10:
                            name = "dianliang";
                            break;
                        default:
                    }
                    List<String> rowData = new ArrayList<>();
                    Row row = sheet2.getRow(i);
                    for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                        String cellInfo = row.getCell(j).toString();
                        if (cellInfo.isEmpty()) {
                            continue;
                        }
                        rowData.add(cellInfo);
                    }
                    if (rowData.size() < 24) {
                        for (int k = 1; k <= 24 - rowData.size(); k++) {
                            rowData.add("0.0");
                        }
                    }
                    summerData.put(name, rowData);

                }
                return summerData;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    办公用电
    照明用电
    消防安防用电
    其他用电

     */

    public static Map readOtherExcel(File file) {
        try {

            Workbook workbook = new XSSFWorkbook(file);
            // Excel的页签数量
            int sheet_size = workbook.getNumberOfSheets();
            Map<String, List<String>> otherData = new HashMap<>();
            for (int index = 0; index < sheet_size; index++) {
                Sheet sheet = workbook.getSheetAt(index);

                for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
                    List<String> rowData = new ArrayList<>();
                    Row row = sheet.getRow(i);

                    for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                        String cellInfo = row.getCell(j).toString();
                        if (cellInfo.isEmpty()) {
                            continue;
                        }
                        rowData.add(cellInfo);
                    }
                    if (i == 0) {
                        otherData.put("bangong", rowData);
                    } else if (i == 1) {
                        otherData.put("zhaoming", rowData);
                    } else if (i == 2) {
                        otherData.put("xiaofang", rowData);
                    } else {
                        otherData.put("other", rowData);
                    }
                }

            }
            return otherData;
        } catch (Exception e) {

        }
        return null;
    }

    public static void main(String[] args) {
        String dir = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        File file = new File(dir + "/demodata/otherdata.xlsx");
        Map<String, List<String>> mapList = readOtherExcel(file);
        int size = mapList.size();
    }
}
